package com.zkx.controller;

import com.zkx.bean.User;
import com.zkx.service.BankService;

import java.io.IOException;
import java.util.Scanner;;

/**
 * @author zkx
 */
//银行业务逻辑
public class BankController {

    private BankService bank = new BankService();

    Scanner scanner = new Scanner(System.in);

    private User loginUser;

    /**
     * 登入页面
     */
    public User login() throws IOException, ClassNotFoundException {
        System.out.println("请输入账号:");
        int id = scanner.nextInt();
        System.out.println("请输入密码:");
        String password = scanner.next();
        loginUser = bank.login(id, password);
        //登入成功
        if(loginUser!=null){
            this.Windows();
        }else{
            System.out.println("账号不存在或者密码错误，请重新登入");
            System.out.println("系统已退出，请重新启动！");
            scanner.close();
            System.exit(0);
        }
        return loginUser;
    }

    /**
     * 注册页面
     */
    public void registerAndLogin() throws IOException, ClassNotFoundException {
        System.out.println("---------欢迎来到XX银行--------");
        System.out.println("已有账号登入，请按1");
        System.out.println("未有账号，注册账号，请按2");
        System.out.println("请输入(1/2):");
        int choice = scanner.nextInt();
        if(choice==1){
            login();
        }else if(choice == 2){
            this.register();
            this.Windows();
        }else{
            System.out.println("选择服务不存在");
            System.out.println("系统已退出");
            scanner.close();
            System.exit(0);
        }
    }

    /**
     * 服务窗口
     */
    public void Windows() throws IOException {
        System.out.println("欢迎您,"+ loginUser.getUserName() +"很高兴为您服务");
        System.out.println("-------------------------------------------");
        System.out.println("              银行服务页面                                                               ");
        System.out.println("              查询输入1                       ");
        System.out.println("              存款输入2                       ");
        System.out.println("              取款输入3                       ");
        System.out.println("              退出输入4                       ");
        System.out.println("-------------------------------------------");
        this.bank();
    }

    /**
     * 循环银行业务逻辑
     */
    public void bank() throws IOException {
        while(true){
            System.out.println("请输入(1~4):");
            int i = scanner.nextInt();
            checkInput(i);
        }

    }

    /**
     * 判断所选择的业务
     */
    public void checkInput(Integer i) throws IOException {
        switch (i){
            //查询
            case 1:
                double money = loginUser.getMoney();
                System.out.println("余额为"+money);
                break;
            //存款
            case 2:
                System.out.println("请输入输入存款金额:");
                double saveMoney = scanner.nextDouble();
                boolean saveFlag = bank.saveMoney(saveMoney);
                if(saveFlag){
                    System.out.println("存款成功,共存"+saveMoney);
                }else{
                    System.out.println("存款失败,存款金额不能为负数");
                }
                break;
            //取款
            case 3:
                System.out.println("请输入输入取款金额:");
                double withdrawMoney = scanner.nextDouble();
                boolean withdrawFlag = bank.withdrawMoney(withdrawMoney);
                if(withdrawFlag){
                    System.out.println("取款成功,剩余余额为"+loginUser.getMoney()+"元");
                }else{
                    System.out.println("取款失败,余额不足，剩余:"+loginUser.getMoney()+"元");
                }
                break;
            //退出系统
            case 4:
                scanner.close();
                bank.exit();
                System.out.println("退出成功,祝您生活愉快！");
                System.exit(1);
                break;
            default:
                System.out.println("没有该项业务");
        }
    }

    /**
     * 注册账户逻辑
     */
    public void register() throws IOException, ClassNotFoundException {
        System.out.println("请输入账号(6为数字):");
        int id = scanner.nextInt();

//        while(!checkId(id)){
//            System.out.println("格式不对，6位数字，请重新输入:");
//            id = scanner.nextInt();
//        }
        System.out.println("请输入密码,6到16位数字字母组合:");
        String password = scanner.next();

        System.out.println("请输入姓名:");
        String name = scanner.next();

        System.out.println("请输入首次存款金额:");
        double money = scanner.nextDouble();

        User user = new User(id, password, money, name);
        //检查账户是否存在
        boolean registerFlag = bank.register(user);
        if(!registerFlag){
            scanner.close();
            System.out.println("注册失败，账户已存在");
            System.out.println("系统已退出，请重新启动！");
            System.exit(0);
        }
        bank.register(user);
        this.loginUser = user;
        System.out.println("注册成功");
    }

    public boolean checkId(Integer id){
        String reg = "^[0-9]{6}$";
        return reg.matches(id + "");
    }


}
